package com.qianxunclub.util;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

public class PropertiesData implements InitializingBean {

	private List<Resource> properties;
	private static Properties prop;
	public void afterPropertiesSet() throws Exception {
		reload();
	}

	public static String getString(String key, String defaultValue){
		if (prop == null) {
			return defaultValue;
		}
		return prop.getProperty(key, defaultValue);
	}
	public static String getString(String key) {
		if (prop == null) {
			return null;
		}
		return prop.getProperty(key);
	}
	public static int getInt(String key, int defaultValue) {
		String value = prop.getProperty(key);
		int returnInt = 0;
		try {
			returnInt = Integer.parseInt(value);
		} catch (Exception e) {
			returnInt = defaultValue;
		}
		return returnInt;
	}
	public static long getLong(String key) {
		return getLong(key, 0);
	}
	public static long getLong(String key, long defaultValue) {
		String value = prop.getProperty(key);
		long returnLong = defaultValue;
		try {
			returnLong = Long.parseLong(value);
		} catch (Exception e) {
		}
		return returnLong;
	}
	
	public static boolean getBoolean(String key) {
		return Boolean.valueOf(prop.getProperty(key));
	}
	public static int getInt(String key) {
		return getInt(key, 0);
	}
	private void reload() throws IOException{
		prop = new Properties();
		for (Resource resource : properties) {
			prop.load(resource.getInputStream());
		}
	}
	public void setProperties(List<Resource> properties) {
		this.properties = properties;
	}
}
